home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-22 | 1.8 KB | 50 lines | [TEXT/CWIE] |
- #include "CParticle.h"
-
- /* This class implements a simple ballistic particle in 2D with position, velocity,
- and acceleration (all of which are "Fixed point Points"). The velocity and
- acceleration are treated as 2D vectors, and their units are pixels per frame and
- pixels per frame per frame, respectively. This conveniently eliminates mass
- considerations in the math. The reason I do this is 'cuz it's faster. There are only
- three routines: InitParticle, which sets the values of the variables; NextStep, which
- moves the particle a time step of 1, calculating the new position and velocity; and
- Step, which takes an integer and moves that many steps. Note that there are no
- drawing routines, and that all three variables are available only to subclasses. */
-
- CParticle* CParticle :: InitParticle(FloatPoint *ipos, FloatPoint *ivel, FloatPoint *iaccel)
- {
- fPosition = *ipos;
- fVelocity = *ivel;
- fAcceleration = *iaccel;
- return this;
- }
-
- void CParticle :: NextStep()
- {
- /* The equation for the new position is: P(T) = Po + VoT + 1/2 AT^^2. Here we assume a
- time step of 1, so it reduces to: P = Po + Vo + A/2, all shifts and adds, which
- are fast and "Fixed-friendly" */
-
- double h, v; // horizontal and vertical positions
-
- h = this->fPosition.h;
- v = this->fPosition.v;
- h = h + this->fVelocity.h + (this->fAcceleration.h / 2); // velocity + half the acceleration
- v = v + this->fVelocity.v + (this->fAcceleration.v / 2);
-
- /* The equation for the new velocity is: V(T) = Vo + AT, and T = 1 so... */
- this->fVelocity.h += this->fAcceleration.h;
- this->fVelocity.v += this->fAcceleration.v;
-
- // Set new position
- fPosition.h = h;
- fPosition.v = v;
- }
-
- void CParticle :: Step(short numsteps)
- {
- // Just single step the given number of times
- while(numsteps-- > 0)
- NextStep();
- }
-
-